'''
@Author: your name
@Date: 2020-02-03 16:36:04
@LastEditTime : 2020-02-03 19:38:51
@LastEditors  : Please set LastEditors
@Description: In User Settings Edit
@FilePath: \pyqt5\demo_020_父子对象内存管理.py
'''
from PyQt5.Qt import *


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('demo_020_父子对象内存管理')
        self.resize(500,500)
        self.setup_ui()

    def setup_ui(self):
        self.父子对象内存管理()
    
    def 父子对象内存管理(self):
        obj1 = QObject()
        obj2 = QObject()

        obj1.setParent(obj2)

        obj1.destroyed.connect(lambda: print('obj1 is destroyed'))
        obj2.destroyed.connect(lambda: print('obj2 is destroyed'))
        
        # 函数执行完但是用私有变量 保存父对象
        self.objitem = obj2

        # 删除父对象时会先删除父控件中的子控件
        del self.objitem


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)

    win = Window()
    win.show()
    sys.exit(app.exec_())
